package com.example.nacosdemo.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.example.nacosdemo.dto.ReduceDTO;
import com.example.nacosdemo.service.ProductService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/product/")
@Slf4j
@AllArgsConstructor
public class ProductController {

    private final ProductService productService;


    @SentinelResource
    @PostMapping("reduce")
    public Boolean reduce(@RequestBody ReduceDTO dto){
        try {
            Boolean flag = productService.reduce(dto);
            return flag;
        }catch (Exception e){
            log.error("e: {}", e);
            return false;
        }
    }

    @PostMapping("reduce_and_change_order")
    public void reduceAndChangeOrder(@RequestBody ReduceDTO dto){
        try {
            productService.reduceAndChangeOrder(dto);
        }catch (Exception e){
            log.error("e: {}", e);
        }
    }

    @PostMapping("reduce_with_dog")
    public String reduceWithDog(@RequestBody ReduceDTO dto){
        try {
            productService.reduceWithDog(dto);
            return "success";
        }catch (Exception e){
            log.error("e: {}", e);
            return e.getMessage();
        }
    }

    @PostMapping("reduce_with_redisson")
    public String reduceWithRedisson(@RequestBody ReduceDTO dto){
        try {
            productService.reduceWithRedisson(dto);
            return "success";
        }catch (Exception e){
            log.error("e: {}", e);
            return e.getMessage();
        }
    }
}
